package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    LogService logService;

    @Override
    public Map<String, Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1)*rows;
        List<Goods> goodsList = goodsDao.listInventory(offSet, rows, codeOrName, goodsTypeId);
        logService.save(new Log(Log.SELECT_ACTION,"分页查询商品存储信息"));
        map.put("total",goodsDao.getTotal(codeOrName,goodsTypeId));
        map.put("rows",goodsList);
        return map;
    }

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1)*rows;
        List<Goods> goodsList = goodsDao.list(offSet, rows, goodsName, goodsTypeId);
        map.put("total",goodsDao.getTotal2(goodsName,goodsTypeId));
        map.put("rows",goodsList);
        logService.save(new Log(Log.SELECT_ACTION,"查询所有商品信息"));
        return map;
    }


    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public ServiceVO save(Goods goods) {
        // 供应商ID为空时，说明是新增操作，需要先判断供应商名字是否存在
        if (goods.getGoodsId() == null){
            goods.setInventoryQuantity(goods.getMinNum());
            goods.setState(0);
            goodsDao.addGoods(goods);
            logService.save(new Log(Log.INSERT_ACTION,"添加商品："+goods.getGoodsName()));
//            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } else // 修改操作
             {
                 goods.setInventoryQuantity(goods.getMinNum());
                 goods.setState(0);
                 goodsDao.updateGoods(goods);
            logService.save(new Log(Log.UPDATE_ACTION,"修改商品:"+goods.getGoodsName()));
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO delete(Integer goodsId) {
        Integer state = goodsDao.getStateById(goodsId);
        if (state == 1){
            logService.save(new Log(Log.DELETE_ACTION,"该商品已入库，不能删除"));
            return new ServiceVO(ErrorCode.STORED_ERROR_CODE,ErrorCode.STORED_ERROR_MESS);
        } else if(state == 2){
                logService.save(new Log(Log.DELETE_ACTION,"该商品有进货或销售单据，不能删除"));
                return new ServiceVO(ErrorCode.HAS_FORM_ERROR_CODE,ErrorCode.HAS_FORM_ERROR_MESS);
        }
            goodsDao.delete(goodsId);
            logService.save(new Log(Log.DELETE_ACTION,"删除商品："));
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    /**
     * 无库存商品列表展示
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */

    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1)*rows;
        List<Goods> goodsList = goodsDao.getNoInventoryQuantity(offSet, rows, nameOrCode);
        map.put("total",goodsDao.getTotal3(nameOrCode));
        map.put("rows",goodsList);
        logService.save(new Log(Log.SELECT_ACTION,"无库存商品列表展示"));
        return map;
    }

    /**
     * 有库存商品列表展示（可以根据商品名称或编码查询）
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1)*rows;
        List<Goods> goodsList = goodsDao.getHasInventoryQuantity(offSet, rows, nameOrCode);
        map.put("total",goodsDao.getTotal4(nameOrCode));
        map.put("rows",goodsList);
        logService.save(new Log(Log.SELECT_ACTION,"有库存商品列表展示"));
        return map;
    }
}
